package net.wangds.yunz.initialize.flow;

import net.wangds.log.helper.LogHelper;
import net.wangds.procengine.ProcResEnum;
import net.wangds.procengine.flow.define.SimpleFlow;
import net.wangds.procengine.flow.define.actor.ActorDef;
import net.wangds.procengine.flow.define.node.FlowNode;
import net.wangds.procengine.flow.define.node.JavaFlowNode;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

import static net.wangds.spring.utils.SpringUtils.getBean;

/**
 * .
 * <p></p>
 *
 * @author 王东石 2020/10/18 10:06.
 */
@Service
@Lazy
//@DependsOn({"stepInitSystems","stepInitApplications","stepInitModules","stepInitMenus"})
public class FlowInitAllRes extends SimpleFlow<ActorDef> {

    public static final String KEY_RES_MAP = "RES_MAP";

    /*
     * 装配流程.
     */
    @Override
    protected synchronized void combine() {
        super.combine();

        this.setFlowId("InitAllRes");
        this.setFlowName("初始化全部资源");

        //输出流程启动日志
        FlowNode ndBegin = new JavaFlowNode(this, (c, a)->{
            LogHelper.dev("启动"+getFlowName()+"过程");
            return ProcResEnum.CONTINUE;
        }, null);

        //init systems
        FlowNode stepInitSyses = getBean(StepInitSystems.class, this, ndBegin);
        //init applications
        FlowNode stepInitApplication = getBean(StepInitApplications.class, this, stepInitSyses);

        //init modules
        FlowNode stepInitModules = getBean(StepInitModules.class, this, stepInitApplication);
        //this.addFlowNode(stepInitModules);

        //init menus
        FlowNode stepInitMenus = getBean(StepInitMenus.class, this, stepInitModules);
        //this.addFlowNode(stepInitMenus);

        FlowNode stepInitAccounts = getBean(StepInitAccount.class, this, stepInitMenus);

        //生成返回结果
        FlowNode ndEnd = new JavaFlowNode(this, (c, a)->{

            Map<String, Object> map = new HashMap<>();

            map.put(StepInitSystems.KEY_SYSES, c.getBean(StepInitSystems.KEY_SYSES));
            map.put(StepInitApplications.KEY_APPS, c.getBean(StepInitApplications.KEY_APPS));
            map.put(StepInitModules.KEY_MODS, c.getBean(StepInitModules.KEY_MODS));
            map.put(StepInitMenus.KEY_MENUS, c.getBean(StepInitMenus.KEY_MENUS));

            c.putBean(KEY_RES_MAP, map);
            return ProcResEnum.CONTINUE;
        }, stepInitAccounts);
        this.addFlowNode(ndEnd);

        // 设置清理过程
        this.setOnHouseKeeping((c,a)-> {
        });

    }
}